#include "ai_handler.hh"

AIHandler::AIHandler ()
{
}

AIHandler::~AIHandler ()
{
}

void
AIHandler::add_object (Unit* o)
{
  std::list<Unit*>::iterator it = find (list_.begin (), list_.end (), o);

  if (it == list_.end ())
    list_.push_back (o);
}

void
AIHandler::del_object (Unit* o)
{
  std::list<Unit*>::iterator it = find (list_.begin (), list_.end (), o);

  if (it != list_.end ())
    list_.erase (it);
}

void
AIHandler::act ()
{
  std::list<Unit*>::iterator it;
  int playerid = 0;
  Vector3f target;

  for (it = list_.begin (); it != list_.end (); ++it)
    {
      playerid = (*it)->playerid ();
      target = find_target (playerid)->pos_get ();
      if (target != Vector3f::zero ())
	(*it)->ai ()->target (target);
      (*it)->ai ()->move ((*it)->pos_get ());
      (*it)->ai ()->act ((*it)->pos_get ());
    }
}

Unit*
AIHandler::find_target (int playerid)
{
  std::list<Unit*>::iterator it;
  for (it = list_.begin (); it != list_.end (); ++it)
    if ((*it)->playerid () != playerid)
      return *it;
  return 0;
}
